<template>
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <h1 class="app-title">贽&nbsp;殿&nbsp;遮&nbsp;那</h1>
                <div class="title-decoration"></div>
            </div>

            <el-form
                    ref="ruleFormRef"
                    :model="ruleForm"
                    :rules="rules"
                    label-position="top"
                    class="login-form"
                    :size="formSize"
                    status-icon
            >
                <el-form-item label="用户名" prop="userName">
                    <el-input
                            v-model="ruleForm.userName"
                            placeholder="请输入您的用户名"
                            prefix-icon="User"
                            clearable
                    />
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input
                            v-model="ruleForm.password"
                            type="password"
                            placeholder="请输入您的密码"
                            prefix-icon="Lock"
                            show-password
                            @keyup.enter="submitForm"
                    />
                </el-form-item>

                <el-form-item class="login-actions">
                    <el-button
                            type="danger"
                            @click="submitForm"

                            class="login-btn"
                    >
                        登 录
                    </el-button>
                </el-form-item>

                <div class="register-link">
                    <span>还没有账号？</span>
                    <el-button type="primary" link @click="toRegister()">立即注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { reactive } from "vue";
import { ElForm, ElMessage } from "element-plus";
import router from "@/router";
import { getUsers } from "@/api/user.js";

export default {
  components: {
    ElForm,
  },
  data() {
    return {
      formSize: "default",
      ruleForm: reactive({
          userId:0,
          userName: "", // 修正字段名
          password: "",
      }),
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "用户名长度需在3-10字符之间", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "密码长度需在6-18字符之间", trigger: "blur" },
        ],
      },
      ruleFormRef: null,
      users: [],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.ruleFormRef = this.$refs.ruleFormRef;
    });
    this.fetchUsers();
  },
  methods: {
    async submitForm() {
      if (!this.ruleFormRef) return;

      await this.ruleFormRef.validate((valid) => {
        if (valid) {
          const user = this.users.find(
              (v) => v.userName === this.ruleForm.userName
          );
          if (!user) {
            this.showMessage("用户名不存在", "error");
            return;
          }
          if (user.password !== this.ruleForm.password) {
            this.showMessage("密码错误", "error");
            return;
          }
          this.showMessage("登录成功", "success");
          sessionStorage.setItem("username", user.userName);
          sessionStorage.setItem("userId", user.id);
          sessionStorage.setItem("role", user.role);
          router.push("./");
        } else {
          this.showMessage("请检查输入是否符合要求", "error");
        }
      });
    },
    toRegister() {
      router.push("./register");
    },
    async fetchUsers() {
      try {
        this.users = await getUsers();
      } catch (error) {
        console.error("获取用户数据失败:", error);
        this.showMessage("无法加载用户数据，请稍后再试", "error");
      }
    },
    showMessage(message, type) {
      ElMessage({
        message,
        type,
        duration: 2000,
      });
    },
  },
};
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed; /* 使用fixed定位确保不出现滚动条 */
    top: 0;
    left: 0;
    right: 0;
    bottom: 0; /* 四个方向都固定为0，填满整个视口 */
    height: 100vh; /* 使用视口高度单位 */
    width: 100vw; /* 使用视口宽度单位 */
    background: linear-gradient(rgba(255, 255, 255, 0.5), rgba(0, 0, 0, 0.5)),
    url('@/assets/img/threeLuoLi.jpg') no-repeat center center fixed;
    background-size: cover;
    padding: 20px; /* 减少内边距 */
    overflow: hidden; /* 隐藏任何可能的溢出内容 */
    box-sizing: border-box; /* 确保padding包含在尺寸内 */
}

.login-card {
    width: 100%;
    max-width: 420px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.75);
    border-radius: 16px;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
    backdrop-filter: blur(8px);
    animation: fadeIn 0.6s ease-out;
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.app-title {
    color: #ff4d4f;
    font-family: '华文行楷', serif;
    font-size: 2.5rem;
    margin: 0;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
    letter-spacing: 2px;
}

.title-decoration {
    height: 4px;
    width: 80px;
    background: linear-gradient(to right, #ff4d4f, #f5222d);
    margin: 10px auto;
    border-radius: 2px;
}

.login-form {
    width: 100%;
}

.login-actions {
    margin-top: 30px;
}

.login-btn {
    width: 100%;
    height: 44px;
    font-size: 16px;
    letter-spacing: 2px;
    background: linear-gradient(to right, #ff4d4f, #f5222d);
    border: none;
    transition: all 0.3s;
}

.login-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(245, 34, 45, 0.4);
}

.register-link {
    text-align: center;
    margin: 20px 0;
    font-size: 14px;
    color: #666;
}







@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (max-width: 480px) {
    .login-card {
        padding: 30px 20px;
        width: 90%;
    }

    .app-title {
        font-size: 2rem;
    }
}

</style>
